package br.com.bancodecurriculos.pessoa.fisica.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;

import br.com.bancodecurriculos.pessoa.comum.dominio.Email;
import br.com.bancodecurriculos.pessoa.comum.dominio.Email.TipoEmail;
import br.com.bancodecurriculos.pessoa.fisica.negocio.PessoaFisicaServices;

public class EmailForm extends JDialog {

	private static final long serialVersionUID = 1L;

	private static final String EMAIL = "Email *";
	private static final String TIPO = "Tipo *";
	private static final String ADICIONAR = "Adicionar";
	private static final String REMOVER = "Remover";
	private static final String SALVAR = "Salvar";

	private static final String CAMPOS_INVALIDOS = "Campos inválidos!";
	private static final String VAZIO = "";

	private final Font FONT = new Font("Microsoft Sans Serif", Font.PLAIN, 14);

	private JLabel lblEmail;
	private JLabel lblTipo;
	private JTextField txtFEmail;
	private JComboBox<String> comboBox;
	private JButton btnAdicionar;
	private JButton btnRemover;
	private JScrollPane scrollPane;
	private DefaultTableModel defaultTableModel;
	private JTable table;
	private JButton btnSalvar;

	private List<Email> emails;

	public EmailForm() {
		super();
		this.emails = new ArrayList<Email>();
		this.init();
	}

	private void init() {
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.setBounds(100, 100, 347, 294);
		this.getContentPane().setLayout(null);
		this.initComponentes();
		this.acoes();
		this.setResizable(false);
		this.setLocationRelativeTo(null);
	}

	private void initComponentes() {
		this.lblEmail = new JLabel(EMAIL);
		this.lblEmail.setFont(FONT);
		this.lblEmail.setBounds(10, 11, 46, 14);
		this.getContentPane().add(this.lblEmail);

		this.lblTipo = new JLabel(TIPO);
		this.lblTipo.setFont(FONT);
		this.lblTipo.setBounds(10, 36, 46, 14);
		this.getContentPane().add(this.lblTipo);

		this.txtFEmail = new JTextField();
		this.txtFEmail.setBounds(64, 9, 267, 20);
		this.getContentPane().add(this.txtFEmail);
		this.txtFEmail.setColumns(10);

		this.comboBox = new JComboBox<String>();
		this.comboBox.setFont(FONT);
		this.comboBox.setBounds(64, 34, 86, 20);
		this.getContentPane().add(this.comboBox);

		this.btnAdicionar = new JButton(ADICIONAR);
		this.btnAdicionar.setFont(FONT);
		this.btnAdicionar.setBounds(70, 65, 97, 25);
		this.getContentPane().add(this.btnAdicionar);

		this.btnRemover = new JButton(REMOVER);
		this.btnRemover.setFont(FONT);
		this.btnRemover.setBounds(177, 65, 97, 25);
		this.getContentPane().add(this.btnRemover);

		this.scrollPane = new JScrollPane();
		this.scrollPane.setFont(FONT);
		this.scrollPane.setBounds(10, 99, 321, 120);
		this.getContentPane().add(this.scrollPane);

		this.defaultTableModel = new DefaultTableModel();
		defaultTableModel.addColumn(EMAIL);
		defaultTableModel.addColumn(TIPO);

		this.table = new JTable(this.defaultTableModel);
		this.table.setFont(FONT);

		this.scrollPane.setViewportView(table);

		this.btnSalvar = new JButton(SALVAR);
		this.btnSalvar.setFont(FONT);
		this.btnSalvar.setBounds(122, 230, 89, 23);
		this.getContentPane().add(this.btnSalvar);
	}

	private void acoes() {
		this.btnAdicionar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent arg0) {
				if (validarCampos()) {
					Email email = criarEmail();
					if (!emails.contains(email)) {
						adicionar(email);
					}
				} else {
					JOptionPane.showMessageDialog(EmailForm.this,
							CAMPOS_INVALIDOS, ADICIONAR,
							JOptionPane.ERROR_MESSAGE);
				}
			}
		});

		this.btnSalvar.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				dispose();
			}
		});
	}

	private boolean validarCampos() {
		if (VAZIO.equals(this.txtFEmail.getText())
				|| this.txtFEmail.getText() == null) {
			return false;
		}
		return true;
	}

	private Email criarEmail() {
		String valor = txtFEmail.getText();
		String tipo = txtFEmail.getSelectedText();
		try {
			TipoEmail tipoEmail = PessoaFisicaServices.getInstance()
					.getTipoEmailByNome(tipo);
			return new Email(valor, tipoEmail);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	private void adicionar(Email email) {
		this.emails.add(email);
		this.carregarTabela(this.emails);
	}

	private void carregarTabela(List<Email> emails) {
		this.defaultTableModel.setRowCount(0);
		for (Email email : emails) {
			this.defaultTableModel.addRow(new Object[] { email.getValor(),
					email.getTipoEmail().getNome() });
		}
	}

	public List<Email> showDialog() {
		this.setVisible(true);
		return this.emails;
	}
}
